from network_manager import PortManager
import os


class DockerDjangoTask():
    def __init__(self, file_path, port_manager):
        self.source_file_path = "docker_django_sample/"
        self.target_file_path = file_path
        self.port_manager = port_manager


    def start(self):
        if not os.path.exists(self.target_file_path):
            os.system("mkdir " + self.target_file_path)
        self.create_requirements()
        self.create_dockerfile()
        self.create_docker_compose_yaml()
        self.create_project()
        os.system("cd " + self.target_file_path + " && docker-compose up -d")


    def stop(self):
        os.system("cd " + self.target_file_path + " && docker-compose stop")


    def create_project(self):
        project_name = "django_sample"
        os.system("cd " + self.target_file_path + " && docker-compose " + 
            "run web django-admin.py startproject " + project_name + " .")
        
        # modify settings.py database setting
        file_name = self.target_file_path + project_name + "/settings.py"
        with open(file_name) as f:
            file_content = f.readlines()
        new_content = []

        i = 0
        while i != len(file_content):
            if "DATABASES" in file_content[i]:
                new_content.append(file_content[i])
                i += 1
                new_content.append(file_content[i])
                i += 1
                if "ENGINE" in file_content[i]:
                    new_line = file_content[i].replace('django.db.backends.sqlite3',
                        'django.db.backends.postgresql_psycopg2')
                    new_content.append(new_line)
                    i += 1
                if "NAME" in file_content[i]:
                    new_line = file_content[i].replace("os.path.join(BASE_DIR, 'db.sqlite3')",
                        "'postgres'")
                    new_content.append(new_line)
                new_content.append("        'USER': 'postgres',\n")
                new_content.append("        'HOST': 'db',\n")
                new_content.append("        'PORT': 5432,\n")
            else:
                new_content.append(file_content[i])
            i += 1

        with open(file_name, "w") as f:
            file_content = f.writelines(new_content)


    def create_requirements(self):
        file_name = "requirements.txt"
        file_content = self.read_to_program(self.source_file_path + file_name)
        self.write_to_file(self.target_file_path + file_name, file_content)


    def create_dockerfile(self):
        file_name = "Dockerfile"
        file_content = self.read_to_program(self.source_file_path + file_name)
        self.write_to_file(self.target_file_path + file_name, file_content)


    def create_docker_compose_yaml(self):
        file_name = "docker-compose.yml"
        file_content = self.read_to_program(self.source_file_path + file_name)
        file_content = file_content.replace("host_port", str(self.port_manager.assign()))

        self.write_to_file(self.target_file_path + file_name, file_content)


    def read_to_program(self, file_name):
        with open(file_name) as f:
            file_content = f.read()
        return file_content


    def write_to_file(self, file_name, file_content):
        with open(file_name, "w") as f:
            f.write(file_content)